﻿//文件指定编译编码UTF-8
#pragma execution_character_set("utf-8")

#include "widget.h"
#include "ui_widget.h"

#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    udpSocket = new QUdpSocket(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_setBtn_clicked()
{
    QString ipStr = ui->ipEdit->text();
    quint16 port = ui->portEdit->value();
    float freq = ui->freqEdit->value();

    if (ipStr.isEmpty()) {
        QMessageBox::warning(this, "错误", "请输入有效的IP地址");
        return;
    }

    if (port == 0) {
        QMessageBox::warning(this, "错误", "请输入有效的端口号");
        return;
    }

//    double frequency = freqStr.toDouble(&ok);
//    if (frequency < 0) {
//        QMessageBox::warning(this, "错误", "请输入有效的频率值");
//        return;
//    }

    int32_t frequency = freq * 1000;
    QByteArray sendData;
    sendData.resize(4);
    memcpy(sendData.data(),&frequency,4);

    qint64 sent = udpSocket->writeDatagram(sendData, QHostAddress(ipStr), port);
    if (sent == -1) {
        QMessageBox::critical(this, "错误", "发送失败");
    } else {
        QMessageBox::information(this, "成功", QString("发送成功：%1 Hz").arg(frequency));
    }
}
